﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoMapper;

namespace AlmKickstart.Web.Infrastructure
{
	public class AutoMapAttribute : ActionFilterAttribute
	{
		private Type sourceType;
		private Type destType;

		public AutoMapAttribute(Type sourceType, Type destType)
		{
			this.sourceType = sourceType;
			this.destType = destType;
		}

		public override void OnActionExecuted(ActionExecutedContext filterContext)
		{
			base.OnActionExecuted(filterContext);
			var model = filterContext.Controller.ViewData.Model;
			object viewModel = Mapper.Map(model, sourceType, destType);
			filterContext.Controller.ViewData.Model = viewModel;
		}
	}
}